package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteContentTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;



public class UpRomTask extends AbsSendWriteContentTask {

    private static final byte CMD = 0x33;

    @Override
    protected int serialNo() {
        return 0x3300;
    }

    public UpRomTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask, byte[] romData) {
        super(taskExecutor, UpRomTask.class.getName(), mTaskManager, bluetoothSocket, mReceiveTask);
        this.romData = romData;
        romSize = romData.length;
    }

    private int romSize = 0;

    private byte[] romData;

    @Override
    protected void onPreData() {
        super.onPreData();












    }

    @Override
    protected boolean checkTickIsMe(CMDEntity cmdEntity) {
        return cmdEntity.getNum() == CMD << 8;
    }

    @Override
    protected byte[] onGetRequestCMDContent() {
        
        byte[] dataSizeBytes = NumberHelp.intToBytesDesc(romSize);
        
        
        
        return new byte[]{dataSizeBytes[2], dataSizeBytes[3], (byte) 0xB8, (byte) 0xFC, (byte) 0xD0, (byte) 0xC2, (byte) 0xB4, (byte) 0xFA, (byte) 0xC2, (byte) 0xEB, (byte) 0xA3, (byte) 0xAC, (byte) 0xC8, (byte) 0xF4, (byte) 0xD7, (byte) 0xBC, (byte) 0xB1, (byte) 0xB8, (byte) 0xBA, (byte) 0xC3, (byte) 0xA3, (byte) 0xAC, (byte) 0xC7, (byte) 0xEB, (byte) 0xBB, (byte) 0xD8, (byte) 0xB4, (byte) 0xF0, (byte) 0xA1, (byte) 0xA3};
    }

    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {
        
        
        
        outDataSize[0] = romSize >> (2 * 8);

        return CMD;
    }

    @Override
    protected boolean fillData(int index, byte[] data) {

        int offset = index * data.length;
        if(offset>=romSize){
            return false;
        }

        int sendSize = data.length;

        for (int i = 0; i < sendSize; i++) {
            int dai = offset + i;
            if (dai >= romData.length) break;
            data[i] = romData[dai];
        }

        return true;
    }

    @Override
    protected byte configFillNul() {
        return 0x00;
    }


}
